package DataBase;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

public class BackupManager {
    public static DbcpBean dbcp;
    static{
    	dbcp = new DbcpBean();
    }
	public static int insert(Backup backup)
	{
		Connection conn = dbcp.getConnection();
		int result = 0;
		if(conn!=null)
		{
			Statement stmt;
			try {
				stmt = conn.createStatement();
				String sql = "insert into tb_Backup (ds_ID,backup_date,backup_source)values('"
			    +backup.getds_ID()+"' , '"
				+backup.getBackup_date()+"' , '"
		        +backup.getBackup_source()+"');"; 
		        result  = stmt.executeUpdate(sql);
			} catch (SQLException e) {
				//System.out.println("AssignmentManager����ʧ�ܣ�");
				e.printStackTrace();
			}
			
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public static ArrayList<Backup> select(String backup_date) {
		// TODO Auto-generated method stub
		Connection conn = dbcp.getConnection();
		ResultSet result = null;
		ArrayList<Backup> backups = new ArrayList<Backup>();
		if(conn != null){
			try {
				Statement stmt = conn.createStatement();
				String sql = null;
				if(backup_date == ""){
					sql = "select * from tb_Backup";
					
				}
				else{
					sql = "select * from tb_Backup where backup_date = '"
						+ backup_date +"';";
				}
				result  = stmt.executeQuery(sql);
				while(result.next()){
					Backup backup = new Backup();
					backup.setds_ID((result.getInt("ds_ID")));
					backup.setBackup_date((result.getString("backup_date")));
					backup.setBackup_source(result.getString("backup_source"));
					backups.add(backup);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				try {
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}			
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return backups;
	}
	
	
	public static void main(String[] args) {
        Backup backup = new Backup();
        backup.setds_ID(4);
        backup.setBackup_date("2010-09-10");
        backup.setBackup_source("D:/ach.txt");
        if(BackupManager.insert(backup) == 0){
			System.out.println("插入失败!");
		}
        System.out.println(BackupManager.select("2010-11-29"));
        
	}

}
